package com.lsh.airticket.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.lsh.airticket.dao.AirrouteDao;
import com.lsh.airticket.dao.FlightDao;
import com.lsh.airticket.dao.SpaceDao;
import com.lsh.airticket.dao.TicketDao;
import com.lsh.airticket.dto.ResponseTicket;
import com.lsh.airticket.dto.ShowTicket;
import com.lsh.airticket.dto.TicketInformation;
import com.lsh.airticket.entity.Airroute;
import com.lsh.airticket.entity.Flight;
import com.lsh.airticket.entity.Space;
import com.lsh.airticket.entity.Ticket;
import com.lsh.airticket.service.TicketService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author lsh
 * @date 2021/2/16
 **/
@Service
public class TicketServiceImpl implements TicketService {

    @Autowired
    TicketDao ticketDao;
    @Autowired
    SpaceDao spaceDao;
    @Autowired
    FlightDao flightDao;
    @Autowired
    AirrouteDao airrouteDao;

    @Override
    public List<ShowTicket> FindTicketByFlightId(String flightId) {
        ArrayList<ShowTicket> showTickets = new ArrayList<>();
        List<Ticket> tickets = ticketDao.findTicketByFlight(flightId);
        for (Ticket ticket : tickets) {
            Space space = spaceDao.findTbSpaceById(ticket.getSpaceId());
            ShowTicket showTicket = new ShowTicket();
            BeanUtil.copyProperties(ticket, showTicket);
            BeanUtil.copyProperties(space, showTicket);

            showTickets.add(showTicket);
        }
        return showTickets;
    }

    @Override
    public TicketInformation findTicketInformation(Integer ticketId, String flightId) {
        TicketInformation ticketInformation = new TicketInformation();
        Flight flight = flightDao.findTbFlightById(flightId);
        Ticket ticket = ticketDao.findTbTicketById(ticketId);
        Space space = spaceDao.findTbSpaceById(ticket.getSpaceId());
        Airroute airroute = airrouteDao.findTbAirrouteById(ticket.getAirrouteId());
        BeanUtil.copyProperties(flight,ticketInformation);
        BeanUtil.copyProperties(ticket,ticketInformation);
        BeanUtil.copyProperties(space,ticketInformation);
        BeanUtil.copyProperties(airroute,ticketInformation);

        return ticketInformation;
    }

    @Override
    public List<ResponseTicket> findResponseTicket(String flightId) {
        List<Ticket> ticketByFlight = ticketDao.findTicketByFlight(flightId);
        ArrayList<ResponseTicket> responseTickets = new ArrayList<>();
        for (Ticket ticket : ticketByFlight) {
            ResponseTicket responseTicket = new ResponseTicket();
            String spaceName = spaceDao.findTbSpaceById(ticket.getSpaceId()).getSpaceName();
            BeanUtil.copyProperties(ticket, responseTicket);
            responseTicket.setSpaceName(spaceName);
            responseTickets.add(responseTicket);
        }
        return responseTickets;
    }

    @Override
    public int insertTicket(Ticket ticket) {
        ticket.setTicketCreatetime(new Date());
        Integer airrouteId = flightDao.findTbFlightById(ticket.getFlightId()).getAirrouteId();
        ticket.setAirrouteId(airrouteId);

        int i = ticketDao.insertTbTicket(ticket);
        return i;
    }

    @Override
    public int deleteTicketById(String ticketId) {
        return ticketDao.deleteTbTicketById(ticketId);
    }

    @Override
    public Ticket findTicketById(Integer ticketId) {
        return ticketDao.findTbTicketById(ticketId);
    }

    @Override
    public int updateTicket(Ticket ticket) {
        return ticketDao.updateTbTicket(ticket);
    }
}
